<?php

namespace App\Tcp;

class Base
{

    /**
     * 常量配置
     */
    const TCP_LOGIN_KEY = 'user:tcp:login';
    const FD_UID_KEY = 'fd:uid';
    const USER_GROUP_KEY = 'user:group:';

    protected static $redisConfig = [
        'host'          => '192.168.0.250',
        'port'          => '6379',
        'auth'          => '',
        'serialize'     => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_NONE,
    ];

    protected static function redis()
    {
        $config = $config ?? self::$redisConfig;
        $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig($config));
        return $redis;
    }

    /**
     * 返回json数据
     */
    protected static function returnJSON($code = 0, $msg = 'SUCCESS', $result = [])
    {
        return json_encode(compact('code', 'msg', 'result'), JSON_UNESCAPED_UNICODE);
    }

    /**
     * 返回成功数据
     */
    protected static function successJSON($result = [], $msg = 'SUCCESS')
    {
        return self::returnJSON(0, $msg, $result);
    }

    /**
     * 返回失败数据
     */
    protected static function errorJSON($code = 1, $msg = 'ERROR', $result = [])
    {
        return self::returnJSON($code, $msg, $result);
    }

}